//
//  GetScanViewController.swift
//  AutoPrint
//
//  Created by 胡玉葱 on 16/10/22.
//  Copyright © 2016年 fatcat. All rights reserved.
//

import UIKit

@objc protocol GetScanViewControllerDelegate {
    @objc optional func scanViewController(_ scanViewController: GetScanViewController, didFinishedGetCode code: String?, orderID: String?)
}

class GetScanViewController: LBXScanViewController {

    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent
    }
    
    var oid:String?
    var delegate: GetScanViewControllerDelegate?
    
    var navbar: UINavigationBar!
    
    var backBtn: UIButton!
    var navTitle: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        navigationController?.navigationBar.tintColor = UIColor.white
        // Do any additional setup after loading the view.
//        navbar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 64))
//        navbar.barStyle = .black
//        navbar.barTintColor = UIColor.orange
//        navbar.tintColor = UIColor.white
//        view.addSubview(navbar)
//        let navItem = UINavigationItem(title: "扫描二维码")
//        navbar.pushItem(navItem, animated: false)
//        
//        navItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(GetScanViewController.close))
        
        
        
        
        let infoLabel = UILabel(frame: CGRect(x: 16, y: 450, width: view.bounds.width - 32, height: 20))
        infoLabel.text = "将二维码放入框中即可自动扫描"
        infoLabel.textColor = UIColor.white
        infoLabel.font = UIFont.systemFont(ofSize: 14)
        infoLabel.textAlignment = .center
        view.addSubview(infoLabel)
        
//        view.bringSubview(toFront: navbar)
        
        backBtn = UIButton(frame: CGRect(x: 16, y: 20, width: 44, height: 44))
        backBtn.setImage(#imageLiteral(resourceName: "navbar_back"), for: .normal)
        backBtn.addTarget(self, action: #selector(GetScanViewController.close), for: .touchUpInside)
        view.addSubview(backBtn)
        
        navTitle = UILabel(frame: CGRect(x: 44, y: 20, width: view.frame.width - 88, height: 44))
        navTitle.font = UIFont.boldSystemFont(ofSize: 18)
        navTitle.textAlignment = .center
        navTitle.textColor = UIColor.white
        navTitle.text = "扫描二维码"
        view.addSubview(navTitle)
        
        
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
//        view.bringSubview(toFront: navbar)
        
        view.bringSubview(toFront: backBtn)
        view.bringSubview(toFront: navTitle)
    }
    
    override func handleCodeResult(arrayResult: [LBXScanResult]) {
        
        
        
        if arrayResult.first != nil{
            
            let resultStr = arrayResult.first?.strScanned
            
            if resultStr != nil{
                print(resultStr!)
                if resultStr!.hasPrefix("mernum") {
                    delegate?.scanViewController?(self, didFinishedGetCode: resultStr, orderID: oid)
                    self.dismiss(animated: true, completion: nil)
                    return
                }
                
            }
            
            
            
//            let alert = UIAlertController(title: "扫描结果", message: arrayResult.first?.strScanned, preferredStyle: .alert)
//            alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
//            self.present(alert, animated: true, completion: nil)
            
//            _ = navigationController?.popViewController(animated: true)
            
            
        }
        
        
        let alert = UIAlertController(title: "扫描失败", message: nil, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: {(action) in
            self.startScan()
        }))
        self.present(alert, animated: true, completion: nil)
        
        
    }
    
    func close(){
        dismiss(animated: true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
